package com.sec.android.app.myfiles.dialog;

import android.app.AlertDialog;
import android.app.Dialog;
import android.app.FragmentManager;
import android.app.FragmentTransaction;
import android.content.DialogInterface;
import android.content.Intent;

import com.sec.android.app.myfiles.R;
import com.sec.android.app.myfiles.activity.SettingsActivity;
import com.sec.android.app.myfiles.log.SamsungAnalyticsLog;
import com.sec.android.app.myfiles.update.APKDownloadManager;

/**
 * Created by hyejung8.kim on 2017-02-22.
 */

public class UpdateDialogFragment extends AbsDialogFragment {
    private static final String DIALOG_TAG = "UpdateDialogFragment_Update";

    private static boolean sHasCloudUpdate = false;

    public static void updateDialogShow(FragmentManager manager, boolean hasCloudUpdate) {
        if (manager.findFragmentByTag(DIALOG_TAG) != null) {
            return;
        }
        UpdateDialogFragment dialog = new UpdateDialogFragment();
        sHasCloudUpdate = hasCloudUpdate;
        dialog.showAllowingStateLoss(manager, DIALOG_TAG);
    }

    @Override
    protected Dialog _createDialog() {
        AlertDialog.Builder builder = new AlertDialog.Builder(getActivity());
        builder.setTitle(getResources().getString(R.string.update_myfiles_header));
        int dialogText = (sHasCloudUpdate ? R.string.update_dialog_include_cloud : R.string.update_dialog_txt);
        builder.setMessage(getResources().getString(dialogText));
        builder.setPositiveButton(getResources().getString(R.string.about_page_update), new DialogInterface.OnClickListener() {
            @Override
            public void onClick(DialogInterface dialog, int whichButton) {
                if (dialog != null) {
                    dialog.dismiss();
                }
                try {
                    Intent intent = new Intent(mContext, SettingsActivity.class);
                    intent.setAction(SettingsActivity.MYFILES_ACTION_START_ABOUT_PAGE);
                    mContext.startActivity(intent);
                } catch (android.content.ActivityNotFoundException e) {
                    e.printStackTrace();
                }
                SamsungAnalyticsLog.sendLog(SamsungAnalyticsLog.ScreenPath.UPDATE_POPUP, SamsungAnalyticsLog.Event.UPDATE, null);
            }
        });
        builder.setNegativeButton(getResources().getString(R.string.cancel), new DialogInterface.OnClickListener() {
            @Override
            public void onClick(DialogInterface dialog, int whichButton) {
                APKDownloadManager.getInstance(mContext.getApplicationContext()).setVersionChecking(false);
                SamsungAnalyticsLog.sendLog(SamsungAnalyticsLog.ScreenPath.UPDATE_POPUP, SamsungAnalyticsLog.Event.CANCEL, null);
            }
        });

        return builder.create();
    }

    /**
     * Shows dialog using commitAllowingStateLoss for committing
     * FragmentTransaction. The only danger is that state of the parent Fragment
     * might omit the fact, that the dialog has been shown.
     */
    public void showAllowingStateLoss(final FragmentManager fm, final String tag) {
        if (fm != null) {
            final FragmentTransaction t = fm.beginTransaction();
            t.add(this, tag);
            t.commitAllowingStateLoss();
        }
    }
}
